
__itcm_start equ 0x01ff0000
__dtcm_start equ 0x02800000

PAGE_4K		EQU 2_01011 << 1
PAGE_8K		EQU 2_01100 << 1
PAGE_16K	EQU 2_01101 << 1
PAGE_32K	EQU 2_01110 << 1
PAGE_64K	EQU 2_00111 << 1
PAGE_128K	EQU 2_10000 << 1
PAGE_256K	EQU 2_10001 << 1
PAGE_512K	EQU 2_10010 << 1
PAGE_1M		EQU 2_10011 << 1
PAGE_2M		EQU 2_10100 << 1
PAGE_4M 	EQU 2_10101 << 1
PAGE_8M 	EQU 2_10110 << 1
PAGE_16M 	EQU 2_10111 << 1
PAGE_32M 	EQU 2_11000 << 1
PAGE_64M 	EQU 2_11001 << 1
PAGE_128M 	EQU 2_11010 << 1
PAGE_256M 	EQU 2_11011 << 1
PAGE_512M 	EQU 2_11100 << 1
PAGE_1G 	EQU 2_11101 << 1
PAGE_2G 	EQU 2_11110 << 1
PAGE_4G 	EQU 2_11111 << 1

ITCM_LOAD		EQU	1<<19
ITCM_ENABLE		EQU	1<<18
DTCM_LOAD		EQU	1<<17
DTCM_ENABLE		EQU	1<<16
DISABLE_TBIT	EQU	1<<15
ROUND_ROBIN		EQU	1<<14
ALT_VECTORS		EQU	1<<13
ICACHE_ENABLE	EQU	1<<12
BIG_ENDIAN		EQU	1<<7
DCACHE_ENABLE	EQU	1<<2
PROTECT_ENABLE	EQU	1<<0

;---------------------------------------------------------------------------------
; DS processor selection
;---------------------------------------------------------------------------------
	AREA     globals,CODE,READONLY
		
	EXPORT	__init_mpu_setup

;---------------------------------------------------------------------------------
__init_mpu_setup
;---------------------------------------------------------------------------------
; turn the power on for M3
;---------------------------------------------------------------------------------
	ldr	r0, =0x04000304 ; REG_POWER
	ldr r1, =0x8203 ; POWER_LCD(0) POWER_2D_A(1) POWER_2D_B(9) POWER_SWAP_LCDS(15)
	strh    r1, [r0]

	ldr	r1, =0x00002078			; disable TCM and protection unit
	mcr	p15, 0, r1, c1, c0

;---------------------------------------------------------------------------------
; Protection Unit Setup added by Sasq
;---------------------------------------------------------------------------------
	; Disable cache
	mov	r0, #0
	mcr	p15, 0, r0, c7, c5, 0		; Instruction cache
	mcr	p15, 0, r0, c7, c6, 0		; Data cache

	; Wait for write buffer to empty 
	mcr	p15, 0, r0, c7, c10, 4

	ldr	r0, =__dtcm_start
	orr	r0,r0,#0x0a
	mcr	p15, 0, r0, c9, c1,0		; DTCM base = __dtcm_start, size = 16 KB

	ldr	r0,=(0x00000000 | 0x20)
	mcr	p15, 0, r0, c9, c1,1		; ITCM base = 0 , size = 32 MB

;---------------------------------------------------------------------------------
; Setup memory regions similar to Release Version
;---------------------------------------------------------------------------------

	;-------------------------------------------------------------------------
	; Region 0 - IO registers
	;-------------------------------------------------------------------------
	ldr	r0,=( PAGE_64M | 0x04000000 | 1)	
	mcr	p15, 0, r0, c6, c0, 0

	;-------------------------------------------------------------------------
	; Region 1 - System ROM
	;-------------------------------------------------------------------------
	ldr	r0,=( PAGE_32K | 0xFFFF0000 | 1)	
	mcr	p15, 0, r0, c6, c1, 0

	;-------------------------------------------------------------------------
	; Region 2 - alternate vector base
	;-------------------------------------------------------------------------
	ldr	r0,=( PAGE_4K | 0x00000000 | 1)	
	mcr	p15, 0, r0, c6, c2, 0

	;-------------------------------------------------------------------------
	; Region 3 - DTCM
	;-------------------------------------------------------------------------
	ldr	r0,=__dtcm_start
	orr	r0,r0,#(PAGE_16K | 1)
	mcr	p15, 0, r0, c6, c3, 0

	;-------------------------------------------------------------------------
	; Region 4 - ITCM
	;-------------------------------------------------------------------------
	ldr	r0,=( PAGE_32M | 0 | 1)
	mcr	p15, 0, r0, c6, c4, 0

	ldr	r0,=0x4004008
	ldr	r0,[r0]
	ands	r0,r0,#0x8000
	bne	dsi_mode
	
	swi	0xf0000

	ldr	r1,=( PAGE_128M | 0x08000000 | 1)	
	cmp	r0,#0
	bne	debug_mode

	ldr	r3,=( PAGE_4M | 0x02000000 | 1)	
	ldr	r2,=( PAGE_16M | 0x02000000 | 1)	
	mov	r8,#0x02400000

	adr	r9,dsmasks
	b	setregions

debug_mode
	ldr	r3,=( PAGE_8M | 0x02000000 | 1)	
	ldr	r2,=( PAGE_8M | 0x02800000 | 1)	
	mov	r8,#0x02800000
	adr	r9,debugmasks
	b	setregions

dsi_mode
	ldr	r1,=( PAGE_8M  | 0x03000000 | 1)	
	ldr	r3,=( PAGE_16M | 0x02000000 | 1)	
	ldr	r2,=( PAGE_16M | 0x0C000000 | 1)	
	mov	r8,#0x03000000
	adr	r9,dsimasks

setregions

	;-------------------------------------------------------------------------
	; Region 5 - DS Accessory (GBA Cart) / DSi switchable iwram
	;-------------------------------------------------------------------------
	mcr	p15, 0, r1, c6, c5, 0

	;-------------------------------------------------------------------------
	; Region 6 - non cacheable main ram
	;-------------------------------------------------------------------------
	mcr	p15, 0, r2, c6, c6, 0

	;-------------------------------------------------------------------------
	; Region 7 - cacheable main ram
	;-------------------------------------------------------------------------
	mcr	p15, 0, r3, c6, c7, 0


	;-------------------------------------------------------------------------
	; Write buffer enable
	;-------------------------------------------------------------------------
	ldr	r0,=2_10000000
	mcr	p15, 0, r0, c3, c0, 0

	;-------------------------------------------------------------------------
	; DCache & ICache enable
	;-------------------------------------------------------------------------
	ldr	r0,=2_10000010
	mcr	p15, 0, r0, c2, c0, 0
	mcr	p15, 0, r0, c2, c0, 1

	;-------------------------------------------------------------------------
	; IAccess
	;-------------------------------------------------------------------------
	ldr	r0,=0x33333363
	mcr	p15, 0, r0, c5, c0, 3

	;-------------------------------------------------------------------------
	; DAccess
	;-------------------------------------------------------------------------
	mcr     p15, 0, r0, c5, c0, 2

	;-------------------------------------------------------------------------
	; Enable ICache, DCache, ITCM & DTCM
	;-------------------------------------------------------------------------
	mrc	p15, 0, r0, c1, c0, 0
	ldr	r1,= ITCM_ENABLE | DTCM_ENABLE | ICACHE_ENABLE | DCACHE_ENABLE | PROTECT_ENABLE
	orr	r0,r0,r1
	mcr	p15, 0, r0, c1, c0, 0

	ldr	r0,=masks
	str	r9,[r0]

	bx	lr

dsmasks
	DCD	0x003fffff, 0x02000000, 0x02c00000
debugmasks
	DCD	0x007fffff, 0x02000000, 0x02800000
dsimasks
	DCD	0x00ffffff, 0x02000000, 0x0c000000

masks	DCD	dsmasks

	EXPORT memCached
memCached
	ldr	r1,masks
	ldr	r2,[r1],#4
	and	r0,r0,r2
	ldr	r2,[r1]
	orr	r0,r0,r2
	bx	lr

	EXPORT memUncached
memUncached
	ldr	r1,masks
	ldr	r2,[r1],#8
	and	r0,r0,r2
	ldr	r2,[r1]
	orr	r0,r0,r2
	bx	lr

	END
